using System;
using Csla;
using Ent.AccesoDatos;
using System.Collections.Generic;
using System.Data;

namespace EntGuayasSLA.BL
{
    [Serializable]
    public class Tecnico : BusinessBase<Tecnico>
    {
        #region Business Methods

        public static readonly PropertyInfo<Guid> IdProperty = RegisterProperty<Guid>(p => p.Id);
        public Guid Id
        {
            get { return GetProperty(IdProperty); }
            set { SetProperty(IdProperty, value); }
        }

        #region Datos de Persona
        
        public static readonly PropertyInfo<string> PersonaIdProperty = RegisterProperty<string>(c => c.PersonaId);
        public string PersonaId
        {
            get { return GetProperty(PersonaIdProperty); }
            set { SetProperty(PersonaIdProperty, value); }
        }
        
        public static readonly PropertyInfo<string> IdentificacionProperty = RegisterProperty<string>(c => c.Identificacion);
        public string Identificacion
        {
            get { return GetProperty(IdentificacionProperty); }
            set { SetProperty(IdentificacionProperty, value); }
        }

        public static readonly PropertyInfo<string> NombreProperty = RegisterProperty<string>(c => c.Nombre);
        public string Nombre
        {
            get { return GetProperty(NombreProperty); }
            set { SetProperty(NombreProperty, value); }
        }

        public static readonly PropertyInfo<string> ApellidoProperty = RegisterProperty<string>(c => c.Apellido);
        public string Apellido
        {
            get { return GetProperty(ApellidoProperty); }
            set { SetProperty(ApellidoProperty, value); }
        }

        public static readonly PropertyInfo<string> DireccionProperty = RegisterProperty<string>(c => c.Direccion);
        public string Direccion
        {
            get { return GetProperty(DireccionProperty); }
            set { SetProperty(DireccionProperty, value); }
        }

        public static readonly PropertyInfo<string> EmailProperty = RegisterProperty<string>(c => c.Email);
        public string Email
        {
            get { return GetProperty(EmailProperty); }
            set { SetProperty(EmailProperty, value); }
        }
        public static readonly PropertyInfo<string> ParroquiaIdProperty = RegisterProperty<string>(c => c.ParroquiaId);
        public string ParroquiaId
        {
            get { return GetProperty(ParroquiaIdProperty); }
            set { SetProperty(ParroquiaIdProperty, value); }
        }
        public static readonly PropertyInfo<string> NacionalidadProperty = RegisterProperty<string>(c => c.Nacionalidad);
        public string Nacionalidad
        {
            get { return GetProperty(NacionalidadProperty); }
            set { SetProperty(NacionalidadProperty, value); }
        }
        #endregion

        #region Datos Tecnico
        public static readonly PropertyInfo<string> SeccionProperty = RegisterProperty<string>(c => c.Seccion);
        public string Seccion
        {
            get { return GetProperty(SeccionProperty); }
            set { SetProperty(SeccionProperty, value); }
        }
        #endregion

        #endregion

        #region Business Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            base.AddBusinessRules();

            //BusinessRules.AddRule(new Rule(IdProperty));
        }

        private static void AddObjectAuthorizationRules()
        {
            // TODO: add authorization rules
            //BusinessRules.AddRule(...);
        }

        #endregion

        #region Factory Methods

        public static Tecnico NewEditableRoot()
        {
            return DataPortal.Create<Tecnico>();
        }

        public static Tecnico GetEditableRoot(Guid id)
        {
            return DataPortal.Fetch<Tecnico>(id);
        }

        public static void DeleteEditableRoot(int id)
        {
            DataPortal.Delete<Tecnico>(id);
        }

        private Tecnico()
        { /* Require use of factory methods */ }

        #endregion

        #region Data Access

        [RunLocal]
        protected override void DataPortal_Create()
        {
            // TODO: load default values
            // omit this override if you have no defaults to set
            base.DataPortal_Create();
        }

        private void DataPortal_Fetch(Guid criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { new Parametro("ec_tec_id", criteria.ToString()) };
            DataSet datos = accesoDatos.EjecutarDataSet("\"OBT_TECNICO\"", parametros);
            if (datos.Tables[0].Rows.Count > 0)
            {
                LoadProperty(IdProperty, Guid.Parse(datos.Tables[0].Rows[0]["tec_id"].ToString()));
                LoadProperty(PersonaIdProperty, datos.Tables[0].Rows[0]["per_id"].ToString());
                LoadProperty(IdentificacionProperty, datos.Tables[0].Rows[0]["per_identificacion"].ToString());
                LoadProperty(NombreProperty, datos.Tables[0].Rows[0]["per_nombre"].ToString());
                LoadProperty(ApellidoProperty, datos.Tables[0].Rows[0]["per_apellido"].ToString());
                LoadProperty(DireccionProperty, datos.Tables[0].Rows[0]["per_direccion"].ToString());
                LoadProperty(EmailProperty, datos.Tables[0].Rows[0]["per_email"].ToString());
                LoadProperty(ParroquiaIdProperty, datos.Tables[0].Rows[0]["par_id"].ToString());
                LoadProperty(NacionalidadProperty, datos.Tables[0].Rows[0]["per_nacionalidad"].ToString()); 
                LoadProperty(SeccionProperty, datos.Tables[0].Rows[0]["tec_seccion"].ToString());

            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_tec_id", ReadProperty(IdProperty)),
                new Parametro("ec_per_id", ReadProperty(PersonaIdProperty)),
                new Parametro("ec_per_identificacion", ReadProperty(IdentificacionProperty)),
                new Parametro("ec_per_nombre", ReadProperty(NombreProperty)),
                new Parametro("ec_per_apellido", ReadProperty(ApellidoProperty)),
                new Parametro("ec_per_direccion", ReadProperty(DireccionProperty)),
                new Parametro("ec_per_email", ReadProperty(EmailProperty)), 
                new Parametro("ec_par_id", ReadProperty(ParroquiaIdProperty)),
                 new Parametro("ec_per_nacionalidad", ReadProperty(NacionalidadProperty)),
                new Parametro("ec_tec_seccion", ReadProperty(SeccionProperty))
            };
            accesoDatos.EjecutarQuery("\"INS_TECNICO\"", parametros);
           
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_tec_id", ReadProperty(IdProperty)),
                new Parametro("ec_per_id", ReadProperty(PersonaIdProperty)),
                new Parametro("ec_per_identificacion", ReadProperty(IdentificacionProperty)),
                new Parametro("ec_per_nombre", ReadProperty(NombreProperty)),
                new Parametro("ec_per_apellido", ReadProperty(ApellidoProperty)),
                new Parametro("ec_per_direccion", ReadProperty(DireccionProperty)),
                new Parametro("ec_per_email", ReadProperty(EmailProperty)),  
                new Parametro("ec_par_id", ReadProperty(ParroquiaIdProperty)),
                 new Parametro("ec_per_nacionalidad", ReadProperty(NacionalidadProperty)),
                new Parametro("ec_tec_seccion", ReadProperty(SeccionProperty))
            };
            accesoDatos.EjecutarQuery("\"ACT_TECNICO\"", parametros);
     
         
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(this.Id);
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Delete(Guid criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_tec_id", ReadProperty(IdProperty)),
                new Parametro("ec_per_id", ReadProperty(PersonaIdProperty))
            };
            accesoDatos.EjecutarQuery("\"ELI_TECNICO\"", parametros);
      
        }

        #endregion
    }
}
